/*
单链表不带头标准c语言实现
*/

#include <stdio.h>
#include <stdlib.h>

#define Status int
#define OK 1
#define ERROR 0

typedef struct node
{
    int data;
    struct node *next;
}Node;

void PushBackList(Node ** list, int data);
Status InsertList(Node ** list, int index, int data);
void DeleteList(Node ** list, int data);
void ListPrint(Node ** list);
void FreeList(Node ** list);

int main()
{
    Node *head = NULL;
    Node ** gg = &head;
    for(int i = 0; i < 10; i++) PushBackList(gg, i);
    ListPrint(gg);

    DeleteList(gg, 0);
    printf("delete 0, L = ");
    ListPrint(gg);

    DeleteList(gg, 9);
    printf("delete 9, L = ");
    ListPrint(gg);

    DeleteList(gg, 3);
    printf("delete 3, L = ");
    ListPrint(gg);

    FreeList(gg);
    printf("free, L = ");
    ListPrint(gg);

    for(int i = 0; i < 10; i++) PushBackList(gg, i);
    printf("new init, L = ");
    ListPrint(gg);

    InsertList(gg, 10, 10);
    printf("insert 10, L = ");
    ListPrint(gg);

    InsertList(gg, 0, -1);
    printf("insert 0, L = ");
    ListPrint(gg);

    return 0;
}

void PushBackList(Node ** list, int data)
{
    Node * newblock = (Node *)malloc(sizeof(Node));
    if(!newblock) return;
    newblock->data = data;
    newblock->next = NULL;

    Node * head = (*list);
    if(head == NULL)
        *list = newblock;
    else{
        while(head->next)
        {
            head = head->next;
        }
        // last
        head->next = newblock;
    }
}

Status InsertList(Node ** list, int index, int data)
{
    int size = 0;
    Node *head = *list;

    if(index == 0)
    {
        Node *newblock = (Node *)malloc(sizeof(Node));
        if(!newblock) return ERROR;
        newblock->data = data;
        newblock->next = head;
        *list = newblock;
        return OK;
    }
    else{
        while(head)
        {
            if(size+1 == index)
            {
                // insert
                Node *newblock = (Node *)malloc(sizeof(Node));
                if(!newblock) return ERROR;
                newblock->data = data;
                newblock->next = head->next;
                head->next = newblock;
                return OK;
            }
            head = head->next;
            size++;
        }
    }
    return ERROR;
}

void DeleteList(Node ** list, int data)
{
    Node *head = *list;
    Node *last = *list;
    while(head)
    {
        if(head->data == data)
        {
            if(head == last) // first
            {
                Node * temp = head;
                *list = head->next;
                free(temp);
                return;
            }
            else{
                last->next = head->next;
                free(head);
                return;
            }
        }
        last = head;
        head = head->next;
    }
}

void ListPrint(Node ** list)
{
    Node *head = *list;
    while(head)
    {
        printf("%d ", head->data);
        head = head->next;
    }
    printf("\n");
}

void FreeList(Node ** list)
{
    Node *head = *list;
    Node *temp = NULL;
    while(head)
    {
        temp = head;
        head = head->next;
        free(temp);
    }
    *list = NULL;
}
